package com.cq.controller;

import com.cq.pojo.Banner;
import com.cq.pojo.Result;
import com.cq.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 悠久的苍穹
 * @Date: 2021/1/20 15:54
 * @Description: 广告控制层
 * @Version: 1.0
 */
@Controller
public class BannerController {

    @Autowired
    BannerService bannerService;

    @GetMapping("banner/getBanners")
    @ResponseBody
    public Result getBanners(@RequestParam int page,
                              @RequestParam int limit) {
        Result result = new Result();
        int count = bannerService.getCount();
        List<Banner> bannerList= bannerService.findBanners(page, limit);
        result.setCount(count);
        result.setData(bannerList);
        result.setCode(0);
        return result;
    }

    @GetMapping("banner/getBannersByWX")
    @ResponseBody
    public Result getBannersByWX() {
        Result result = new Result();
        List<Banner> bannerList = bannerService.findAllBanner();
        result.setCode(200);
        result.setData(bannerList);
        return result;
    }

    @GetMapping("/editBannerUI/{id}")
    public String editBannerUI(@PathVariable("id") int id, Model model){
        Banner banner = bannerService.findBannerById(id);
        model.addAttribute("banner",banner);
        return "app/banner/content";
    }

    @ResponseBody
    @PutMapping("/banner/updateBanner")
    public Result updateBanner(@RequestBody Map<String, Object> map) {
        Result result = new Result();
        Integer id = Integer.parseInt(map.get("id").toString());
        String url = map.get("url").toString();
        String desc = map.get("desc").toString();
        Integer ishow = Integer.parseInt(map.get("ishow").toString());
        Banner banner = new Banner();
        banner.setId(id);
        banner.setUrl(url);
        banner.setDesc(desc);
        banner.setIshow(ishow);
        int i = bannerService.updateBanner(banner);
        if(i==1){
            result.setCode(200);
        }else{
            result.setCode(500);
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/addBanner")
    public Result addBanner(@RequestBody Map<String, Object> map){
        Result result = new Result();
        String url = map.get("url").toString();
        String desc = map.get("desc").toString();
        Integer ishow = Integer.parseInt(map.get("ishow").toString());
        Banner banner = new Banner();
        banner.setType("image");
        banner.setUrl(url);
        banner.setDesc(desc);
        banner.setIshow(ishow);
        int i = bannerService.addBanner(banner);
        if(i==1){
            result.setCode(200);
        }else{
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping("/delBanner/{id}")
    @ResponseBody
    public Result moreLog(@PathVariable("id") String id) {
        Result result = new Result();
        String[] s = id.split(",");
        for (int i = 0; i < s.length; i++) {
            bannerService.deleteBannerById(Integer.parseInt(s[i]));
        }
        result.setCode(200);
        return result;
    }

}
